<!-- Note: Most of the Javascript on this page was cheerfully 
	 lifted from Google Map's sample code for processing directions.
	 It has been modified to make it a JSP, as well as to process multiple
	 waypoints at once.
 -->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*"%>
<%@ page import="com.tachoknight.geocaching.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Geocaching Route Planning</title>
<script
	src=" http://maps.google.com/?file=api&amp;v=2.x&amp;key=ABQIAAAAjyXLt7xcxpmvRyMTawa0XhQgZJysIXBCI10Gq9izJ2r_e-ml4BTB2RTimXNawoCoMiJf_dpijRWoLg"
	type="text/javascript"></script>
<style type="text/css">
body {
	font-family: Verdana, Arial, sans serif;
	font-size: 11px;
	margin: 2px;
}

table.directions th {
	background-color: #EEEEEE;
}

img {
	color: #000000;
}
</style>
<script type="text/javascript">
	/* Google's variables */
    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    /* Our variables */
	var wayPoints = [];
	var pos;
	var transmethod;

	function setWaypoints() {
<%    
List<Waypoint> wayPoints= (List<Waypoint>) request.getAttribute("waypoints");
boolean firstTime = true;
int listPos = 0;
for (Waypoint wayPoint : wayPoints)
{
%>
		wayPoints[<%= listPos++ %>] = "<%=wayPoint.getFormattedWaypointForGM() %>"
<%		
}
%>		
		/* And initialize our variables ... */
		pos = 0;
		transmethod = "d";
		document.getElementById("incrementBox").value = 10;
	}
    
	function initialize() {

		/* The Google Maps initialization */
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map_canvas"));
			gdir = new GDirections(map, document.getElementById("directions"));
			GEvent.addListener(gdir, "load", onGDirectionsLoad);
			GEvent.addListener(gdir, "error", handleErrors);
		}
	}

	function setTransportMethod(method)
	{
		transmethod = method;
		setDirections(parseInt(document.getElementById("incrementBox").value));	
	}
	
	function setDirections(stepping) {
		
		var route = [];
		for ( var i = 0; i < stepping; i++) {
			route[i] = wayPoints[i + parseInt(pos)];
		}
		
		if (transmethod == "w")
			gdir.loadFromWaypoints(route, {getPolyline:true,getSteps:true,travelMode:G_TRAVEL_MODE_WALKING});
		else
			gdir.loadFromWaypoints(route, {getPolyline:true,getSteps:true,travelMode:G_TRAVEL_MODE_DRIVING});	
	}

	function handleErrors() {
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: "
					+ gdir.getStatus().code);
		else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
			alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: "
					+ gdir.getStatus().code);

		else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
			alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: "
					+ gdir.getStatus().code);

		else if (gdir.getStatus().code == G_GEO_BAD_KEY)
			alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: "
					+ gdir.getStatus().code);

		else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
			alert("A directions request could not be successfully parsed.\n Error code: "
					+ gdir.getStatus().code);

		else
			alert("An unknown error occurred.");
	}

	function onGDirectionsLoad(){ 
	      // Use this function to access information about the latest load()
	      // results.

	      // e.g.
	      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
		  // and yada yada yada...
		}

	function goBack()
	{
		/* Input sanity checking */
		var increment = parseInt(document.getElementById("incrementBox").value);
		if (increment < 2)
		{
			document.getElementById("incrementBox").value = 2;
			increment = 2;
		}
		else if (increment > 25)
		{
			document.getElementById("incrementBox").value = 25;
			increment = 25;
		}
		
		if (parseInt(pos) < increment)
			pos = 0;
		else
			pos = pos - increment; 
		
		setDirections(increment);
	}

	function goForward()
	{
		/* Input sanity checking */
		var increment = parseInt(document.getElementById("incrementBox").value);

		if (increment < 2)
		{
			document.getElementById("incrementBox").value = 2;
			increment = 2;
		}
		else if (increment > 25)
		{
			document.getElementById("incrementBox").value = 25;
			increment = 25;
		}
		
		if (parseInt(pos) + increment >= wayPoints.length)
			alert("Last Page");
		else
		{
			pos = pos + increment;
			setDirections(increment);
		}
	}
	
</script>
</head>
<body onload="setWaypoints(); initialize(); setDirections('10');">
<table class="directions">
	<tr>
		<th>Turn-by-Turn Directions</th>
		<th>Map</th>
	</tr>
	<tr>
		<td valign="top">
		<div id="directions" style="width: 275px"></div>
		</td>
		<td valign="top">
		<div id="map_canvas" style="width: 800px; height: 600px"></div>
		</td>
	</tr>
</table>

<input type="radio" name="trans" value="w"
	onClick="setTransportMethod('w');">
Walking
<input type="radio" name="trans" value="d" checked
	onClick="setTransportMethod('d');">
Driving
<p /><input type="submit" name="back" value="Back" onclick="goBack();" />
<input type="submit" name="next" value="Next" onclick="goForward();" />
Increment by: <input type="text" id="incrementBox" />
<p/>
<h2>All caches in the list</h2>
<table>
	<tr>
		<th>Description</th>
		<th>Name</th>
		<th>Latitude/Longitude</th>
	</tr>
	<%
		for (Waypoint wayPoint : wayPoints)
		{
	%>
	<tr>
		<td><%=wayPoint.getDescription()%></td>
		<td><%=wayPoint.getName()%></td>
		<td><%=wayPoint.getLatitude()%>/<%=wayPoint.getLongitude()%></td>
	</tr>
	<%
		}
	%>
</table>
</body>
</html>